﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace myTravian
{
    public partial class traCombobox : UserControl
    {
        public traCombobox()
        {
            InitializeComponent();
        }

        private void traCombobox_Load(object sender, EventArgs e)
        {

        }

        public string GetDisplayText()
        {
            return comboBox1.Text;
        }

        public DataRow GetSelectItem()
        {
            DataRowView rv = comboBox1.SelectedItem as DataRowView;
            return rv.Row ;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="pos">指定要顯示的欄位順序</param>
        public void SetControlData(DataTable dt,int colPos)
        {
            if (dt == null) return;
            int iCol = 0;
            iCol = (colPos >= dt.Columns.Count ? 0 : colPos);
            comboBox1.DataSource = null;
            comboBox1.Items.Clear();
            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = dt.Columns[iCol].ColumnName;
        }

        /// <summary>
        /// 沒有給欄位名或欄位順序，預設給0
        /// </summary>
        /// <param name="dt"></param>
        public void SetControlData(DataTable dt)
        {
            SetControlData(dt, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="colName">欄位名，如果不存在也預設用0</param>
        public void SetControlData(DataTable dt, string colName)
        {
            if (dt == null) return;
            int i = 0;
            bool ok = false;
            foreach (string col in dt.Columns)
            {
                if (col.Equals(colName))
                {
                    ok = true;
                    break;
                }
                i++;
            }
            SetControlData(dt,(ok?i:0));
        }
    }
}
